using UnityEngine;
using Network;

public class ProcLogin : Proc{
    public ProcLogin()
        : base(ProcType.PROC_LOGIN)
    {
        
    }

    public override void process()
    {
        NetManager.Instance.Tick(Time.deltaTime);
    }

    public override void onBegin(object[] param)
    {
        RegistUIEvent();
        RegistNetworkEvent();
    }

    public override void onEnd()
    {
        UnregistUIEvent();
        UnregistNetworkEvent();
    }

    void RegistUIEvent()
    {
        UIEventManager.Instance.RegistEventHandler(UIEventName.OnClickLoginButton, beginLogin);
    }

    void RegistNetworkEvent()
    {
        NetManager.Instance.RegistTcpMsg(OpCode.login_toc, onLogin); 
    }

    void UnregistUIEvent()
    {
        UIEventManager.Instance.UnRegistEventHandler(UIEventName.OnClickLoginButton);
    }

    void UnregistNetworkEvent()
    {
        NetManager.Instance.UnregistTcpMsg(OpCode.login_toc);
    }

    private void beginLogin(object[] param)
    {
        NetManager.Instance.sendTcpMsg(OpCode.login_tos, ProtoUtil.SerializeProto(new login_tos()));
    }

    private void onLogin(MsgObject f_msg)
    {
        login_toc loginMsg = ProtoUtil.DeserializeProto<login_toc>(f_msg.msgBody);
        Mainloop.Instance.MyPlayerID = loginMsg.player_id;

        Mainloop.Instance.ctrl.changeProc(ProcType.PROC_LOBBY);
    }

}
